package com.iot.utils;

import com.iot.commons.PageResult;
import com.iot.commons.ResponseResult;
import com.iot.enums.ResultEnum;
import com.iot.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

public class ResponseResultUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseResultUtil.class);

    public ResponseResultUtil() {
    }

    public static <T> ResponseResult<T> success() {
        ResponseResult<T> responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.SECCESS.getCode());
        responseResult.setMessage(ResultEnum.SECCESS.getMessage());
        return responseResult;
    }

    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> responseResult = success();
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> success(String message) {
        ResponseResult<T> responseResult = success();
        responseResult.setMessage(message);
        return responseResult;
    }

    public static <T> ResponseResult<T> success(T data, String message) {
        ResponseResult<T> responseResult = success();
        responseResult.setData(data);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static <T> ResponseResult<PageResult<T>> success(long total, List<T> rows) {
        ResponseResult<PageResult<T>> responseResult = success();
        PageResult<T> pageResult = new PageResult(total, rows);
        responseResult.setData(pageResult);
        return responseResult;
    }

    public static <T> ResponseResult<PageResult<T>> success(String message, long total, List<T> rows) {
        ResponseResult<PageResult<T>> responseResult = success(total, rows);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static ResponseResult failure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.FAILURE.getCode());
        responseResult.setMessage(ResultEnum.FAILURE.getMessage());
        return responseResult;
    }

    public static ResponseResult paramFailure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.INPUT_PARAM_ERROR.getCode());
        responseResult.setMessage(ResultEnum.INPUT_PARAM_ERROR.getMessage());
        return responseResult;
    }


    public static ResponseResult tokenFailure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.SIGN_TOKEN_MISS.getCode());
        responseResult.setMessage(ResultEnum.SIGN_TOKEN_MISS.getMessage());
        return responseResult;
    }

    public static ResponseResult authFailure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.AUTH_FAILURE.getCode());
        responseResult.setMessage(ResultEnum.AUTH_FAILURE.getMessage());
        return responseResult;
    }

    public static ResponseResult authLoginFailure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.AUTH_LOGIN_FAILURE.getCode());
        responseResult.setMessage(ResultEnum.AUTH_LOGIN_FAILURE.getMessage());
        return responseResult;
    }

    public static ResponseResult authInfoFailure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.AUTH_INFO_FAILURE.getCode());
        responseResult.setMessage(ResultEnum.AUTH_INFO_FAILURE.getMessage());
        return responseResult;
    }

    public static ResponseResult findFailure() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(ResultEnum.FIND_FAILURE.getCode());
        responseResult.setMessage(ResultEnum.FIND_FAILURE.getMessage());
        return responseResult;
    }

    public static ResponseResult failure(String msg) {
        ResponseResult responseResult = failure();
        responseResult.setMessage(msg);
        return responseResult;
    }

    public static ResponseResult failure(String msg, Integer code) {
        ResponseResult responseResult = failure();
        responseResult.setMessage(msg);
        responseResult.setCode(code);
        return responseResult;
    }

    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);

        try {
            throwable.printStackTrace(pw);
            String var3 = sw.getBuffer().toString();
            return var3;
        } catch (Exception var7) {
        } finally {
            pw.close();
        }

        return "";
    }

    public static BaseException exception(String msg) {
        return new BaseException(msg);
    }

    public static BaseException exception(String msg, int code, String detail) {
        return new BaseException(msg, code, detail);
    }

    public static BaseException exception(String msg, int code, String detail, Throwable error) {
        return new BaseException(msg, code, detail, error);
    }

    public static <T> T getData(ResponseResult<T> responseResult) {
        LOGGER.info(responseResult == null ? "responseResult->null" : responseResult.toString());
        return responseResult != null && ResultEnum.SECCESS.getCode().equals(responseResult.getCode()) ? responseResult.getData() : null;
    }
}
